/*
 * SelectTag.java
 *
 * Created on 2007年7月19日, 下午5:32
 *
 * 花开堪折须直折，莫待无花空折枝！
 *
 */

package com.ecomm.util.taglibs;
import com.ecomm.util.EcomTagLib;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>ppliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @data  2007年7月19日, 下午5:32
 */
public class SelectTag extends TagSupport {
  private String name = null;
  private String nonull = null;
  private String value = null;
  private String onchange = null;
  private String permission = "readonly";
  public int doStartTag() throws JspException {
    HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
    HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();
    try {
      
      
      EcomTagLib stl = (EcomTagLib)request.getAttribute("ecomTaglib");
      if(stl !=null ){
        String text =  stl.select(getName(),getValue(),getOnchange(),getPermission());
        
        String selectTagValue =(String) stl.getFiledValue(getName());
        if(selectTagValue != null && !selectTagValue.equals("")){
          selectTagValue = selectTagValue;
        }else if(this.getValue() != null && !this.getValue().equals("")){
          selectTagValue  = this.getValue();
        }
        request.setAttribute("selectTag.value",selectTagValue);
        request.setAttribute("selectTag.name",this.getName());
        request.setAttribute("selectTag.permission",this.getPermission());
        
        if(text != null && !stl.getlogic(getName(),getPermission())){
          pageContext.getOut().print(text);
          
        }
        return SKIP_PAGE;
      }
    } catch (IOException ioe) {
      throw new JspTagException("ecomlib tag failed");
    }
    return EVAL_BODY_INCLUDE;
  }
  /**
   * Does the specified value match one of those we are looking for?
   *
   * @param value Value to be compared.
   */
  public boolean isMatched(String value) {
    if ((this.getValue() == null) || (value == null)) {
      return false;
    }
    
    if (value.equals(this.getValue()))
      return true;
    
    return false;
  }
  public int doEndTag() {
    try {
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();
      
      EcomTagLib stl = (EcomTagLib)request.getAttribute("ecomTaglib");
      StringBuilder sb = new StringBuilder();
      
      if(!stl.getlogic(getName(),getPermission())){
        sb.append("</select>");
        
        if(this.getNonull() != null && !this.getNonull().equals(""))
          sb.append(" <span class=\"interim\"><font color=\"red\"> *</font></span> ");
      }
      pageContext.getOut().print(sb.toString());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    
    return EVAL_PAGE;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public String getNonull() {
    return nonull;
  }
  
  public void setNonull(String nonull) {
    this.nonull = nonull;
  }
  
  public String getValue() {
    return value;
  }
  
  public void setValue(String value) {
    this.value = value;
  }
  
  public String getOnchange() {
    return onchange;
  }
  
  public void setOnchange(String onchange) {
    this.onchange = onchange;
  }
  
  public String getPermission() {
    return permission;
  }
  
  public void setPermission(String permission) {
    this.permission = permission;
  }
  
}
